package io.ede.modules.ede.controller;

import java.io.ByteArrayInputStream;
import java.util.*;

import io.ede.common.exception.RRException;
import io.ede.common.utils.XmlParser;
import io.ede.common.validator.ValidatorUtils;
import io.ede.modules.ede.entity.ProjectEntity;
import io.ede.modules.oss.cloud.OSSFactory;
import io.ede.modules.oss.entity.SysOssEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.ede.modules.ede.entity.VideoEntity;
import io.ede.modules.ede.service.VideoService;
import io.ede.common.utils.PageUtils;
import io.ede.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-08-27 13:28:06
 */
@RestController
@RequestMapping("ede/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ede:video:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = videoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ede:video:info")
    public R info(@PathVariable("id") Integer id) {
        VideoEntity video = videoService.getById(id);

        return R.ok().put("video", video);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ede:video:save")
    public R save(@RequestBody VideoEntity video) {
        video.setCreateTime(new Date());
        videoService.save(video);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ede:video:update")
    public R update(@RequestBody VideoEntity video) {
        ValidatorUtils.validateEntity(video);
        video.setUpdateTime(new Date());
        videoService.updateById(video);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ede:video:delete")
    public R delete(@RequestBody Integer[] ids) {
        videoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 获取所有视频
     * @return
     */
    @RequestMapping("/getVideos")
    public R getVideos() {
        List<VideoEntity> videoList = (List<VideoEntity>) videoService.listByMap(new HashMap<>());
        return R.ok().put("videoList", videoList);
    }

    /**
     * 上传视频
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public R uploadMusic(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return R.ok().put("url", url);

    }
}
